var gulp = require('gulp');

/*引入gulp组件*/
var less = require('gulp-less'),
	cssmin = require('gulp-minify-css'),
	rename = require('gulp-rename'),
	connect = require('gulp-connect'),
	uglify = require('gulp-uglify'),
	imagemin = require('gulp-imagemin'),
	livereload = require('gulp-livereload'),
	spriter = require('gulp-css-spriter')

/*配置路径*/
var path = {
	src : "src/",
	css : "src/css/**/*.css",
	less : "src/less/**/*.less",
	js : "src/js/**/*.js",
	images : "src/images/**/*",
	dist : "dist"
}

/*配置任务*/

//编译Less
// gulp.task('less', function(){
//   gulp.src(path.less)
//         .pipe(less())
//         .pipe(gulp.dest(path.dist + "/css"));
// });

//压缩css
gulp.task('mincss', function () {
	gulp.src(path.css)
		.pipe(cssmin())
		.pipe(rename({suffix: '.min'}))
		.pipe(gulp.dest(path.dist + "/css"));
});

//压缩js
// gulp.task('minjs', function() {
//     gulp.src(path.js)
//         .pipe(rename({suffix: '.min'}))   //rename压缩后的文件名
//         .pipe(uglify())    //压缩
//         .pipe(gulp.dest(path.dist + "/js"));  //输出
// });

//压缩图片
gulp.task('imagesmin', function(){
	gulp.src(path.images)
        .pipe(imagemin())
        .pipe(gulp.dest(path.dist + "/images"))
})

//压缩css，雪碧图合并
gulp.task('spriter', function (done) {
  var timestamp = +new Date();
  gulp.src(path.css)
    .pipe(spriter({
      	spriteSheet: 'dist/images/spritesheet/' + timestamp + '.png',// 生成的spriter的位置
      	pathToSpriteSheetFromCSS: '../images/spritesheet/' + timestamp + '.png',// 生成样式文件图片引用地址的路径
      	spritesmithOptions: {
        	padding: 10
      	}
    }))
    .pipe(cssmin())
	  .pipe(rename({suffix: '.min'}))
  	.pipe(gulp.dest(path.dist + "/css"))
    .on('end', done);
});

//服务器
gulp.task('connect', function() {
  connect.server({
  	root: './',
  	port: 8888,
  	livereload : true
  });
})

//livereload
gulp.task('livereload', function () { 
	gulp.src(path.src + '**/*.*')
    	.pipe(connect.reload());
});

gulp.task('watch', function() {
  //监听生产环境目录变化d
  	gulp.watch(path.src + '**/*.*',['livereload']);
})

//开发任务
gulp.task('run',['connect','watch'])
